<!--
 * @Author: guo wei
 * @LastEditors: [guo wei]
 * @LastEditTime: 2024-01-04 20:38:33
 * @Description: 
-->
<template>
  <div class="color-wrap">
    <el-button type="primary" @click="handleAdd">新增档次</el-button>
    <el-table
      :data="tableData"
      style="width: 800px; margin-top: 20px"
      row-key="id"
      border
      default-expand-all
    >
      <el-table-column prop="gradeName" label="档次" align="center">
        <template #default="scope">
          {{ scope.row.gradeName }}
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template #default="scope">
          <el-button size="small" @click="handleEdit(scope.$index, scope.row)"
            >编辑</el-button
          >
          <el-button
            size="small"
            type="danger"
            @click="handleDelete(scope.$index, scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-dialog
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      v-model="dialogVisible"
      :title="dialogTitle"
      width="30%"
    >
      <div>
        <el-form
          ref="ruleFormRef"
          :model="ruleForm"
          :rules="rules"
          label-width="120px"
          class="demo-ruleForm"
        >
          <el-form-item label="档次名称" prop="name">
            <el-input v-model="ruleForm.name" />
          </el-form-item>
        </el-form>
      </div>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="confirmCreateType">
            确认
          </el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>
<script setup>
import { ref, reactive } from "vue";
import {
  gradeInfos,
  addGradeInfos,
  modifyGradeInfos,
  deleteGradeInfos,
} from "@/api/building-type";
import { ElMessage, ElMessageBox } from "element-plus";
const dialogVisible = ref(false);
const ruleFormRef = ref();
const ruleForm = reactive({
  name: "",
});
const dialogTitle = ref("添加档次");
const dialogType = ref("add");
const currentRowId = ref("");
const currentRow = ref({});
const rules = reactive({
  name: [{ required: true, message: "请输入名称", trigger: "blur" }],
});
const handleEdit = (index, row) => {
  console.log(index, row);
  dialogVisible.value = true;
  dialogTitle.value = "编辑档次";
  dialogType.value = "edit";
  currentRowId.value = row.id;
  currentRow.value = row;
  ruleForm.name = row.gradeName;
};
const handleDelete = (index, row) => {
  ElMessageBox.confirm("确定删除吗?", "Warning", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(async () => {
      const res = await deleteGradeInfos({
        gradeInfoId: row.gradeInfoId,
        recordVersion: row.recordVersion,
      });
      if (res.code == 200) {
        ElMessage({
          type: "success",
          message: "删除成功",
        });
        getListData();
      }
    })
    .catch(() => {
      ElMessage({
        type: "info",
        message: "取消删除",
      });
    });
};
const handleAdd = () => {
  dialogVisible.value = true;
  dialogTitle.value = "添加档次";
  dialogType.value = "add";
  currentRowId.value = "";
  currentRow.value = {};
  ruleForm.name = "";
};
const tableData = ref([]);
const getListData = async () => {
  const params = { count: true, pageId: 1, pageSize: 20 };
  try {
    const res = await gradeInfos(params);
    if (res.code == 200) {
      tableData.value = res.result.list || [];
      console.log("tabledata", res);
    }
  } catch (error) {}
};
getListData();
const confirmCreateType = async () => {
  if (!ruleFormRef.value) return;
  await ruleFormRef.value.validate(async (valid, fields) => {
    if (valid) {
      if (dialogType.value == "add") {
        try {
          const res = await addGradeInfos({ gradeName: ruleForm.name });
          console.log("res", res);
          if (res.code == 200) {
            dialogVisible.value = false;
            getListData();
          }
        } catch (error) {}
      } else if (dialogType.value == "edit") {
        try {
          const res = await modifyGradeInfos({
            gradeName: ruleForm.name,
            gradeInfoId: currentRow.value.gradeInfoId,
            recordVersion: currentRow.value.recordVersion,
          });
          console.log("res", res);
          if (res.code == 200) {
            dialogVisible.value = false;
            getListData();
          }
        } catch (error) {}
      }
    } else {
      console.log("error submit!", fields);
    }
  });
};
</script>
<style lang="scss" scoped>
.card-wrap {
  margin: 20px;
}
</style>
